package server;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class ConnectionThread extends Thread {

	private ArrayList<Participant> pl;
	private ArrayList<ReceiveThread> rtl;
	private ServerSocket ss;
	private int maxParticipants;
	private ReceiveMonitor rm;

	public ConnectionThread(ArrayList<Participant> pl, ServerSocket ss,
			int maxParticipants, ReceiveMonitor rm, ArrayList<ReceiveThread> rtl) {
		this.pl = pl;
		this.ss = ss;
		this.maxParticipants = maxParticipants;
		this.rm = rm;
		this.rtl = rtl;

	}

	public void run() {

		int i = 1;
		while (true) {
			try {

				while (pl.size() < maxParticipants) {
					Socket temp = ss.accept();
					Participant p = new Participant("Player" + i, temp);
					pl.add(p);
					i++;
					ReceiveThread rt = new ReceiveThread(p, rm);
					rtl.add(rt);
					rt.start();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
